/**
 *  tp1-2
 *  Author: thainh
 *  Description: 
 */

model tp1

global {
// define variables globales pour l'environnement
	int width_and_height_of_environment min:50 max:1000 <- 100;		
	
// define variables globales pour environnement pour les arbres	
	int number_of_tree max: 1000 <- 100;
	float size_of_tree max: 10.0 <- 8.0;
	int burning_range max: 50 <-2; // Dans ce range, les autre arbres est burning aussi				
	int tree_burning_time <- 500; // Apres ce temp, les arbres est mort		
	string image_tree_path <- '../images/arbres.png';
	int over_4m <- 0; // Pout compter les arbres qui plus grand 4m
	float tree_propagation_probability <- 0.5 max: 1;
	int tree_drying_time <- 200;
	int tree_propagating_time <- 100;
	
	
// define variables globales pour environnement pour les pompiers
    int number_of_pompier max: 20 <- 3;
    float size_of_pompier <- 7.0;
    float speed_of_pompier <- 2.0;
    float range_of_pompier max: 100.0 <- 5.0; // Dans ce range, les pompiers est combattre le feu   
    float capacity_of_pompier <- 15.0; //capacité éteindre un incendie de pompier 	 
    string image_pompier_path <- '../images/fireman_dark.gif';
    int fireman_watering_distance <- 1;
    	
// Cree des list de l'arbres et le pompier
	init{
		create tree number: number_of_tree{	// list de l'arbres
			set size <- rnd (size_of_tree);
			if ( size > 4 ) {
				set over_4m <- over_4m + 1;			
			} 				
		}
		create pompier number: number_of_pompier{ // list du pompier								
		}
	}
}

environment height:width_and_height_of_environment width:width_and_height_of_environment torus: false;

entities {

// objet l'arbres	
	species name:tree skills:[situated,visible] control: fsm {
		rgb color <- [0,200 + rnd (55), 0] as rgb;		
		float size <- rnd(size_of_tree) as float;
		point location <- point([rnd (width_and_height_of_environment -2*size_of_tree) +size_of_tree, rnd (width_and_height_of_environment -2*size_of_tree) +size_of_tree]);
		int range <- burning_range;	
		int burn_duration <- 0;
		int protect_duration <- 0;
		int proba_propagation <- tree_propagation_probability; // propagation de feu		
		geometry shape <- circle(size) simplification (0.1);
				
		state intact initial : true {												
			set color <- rgb('green');
			set burn_duration <- 0;
			transition to: burning when: flip(0.0001); // prendre un arbres est feu au hasard.
		}
		
		state protected {
			set protect_duration <- protect_duration + 1;
		}
		
		state burning  {
			set color <- [255,200 + rnd (55), 0] as rgb;
			set burn_duration <- burn_duration +1;
			transition to: destroyed when: burn_duration > tree_burning_time; // Il est mort
			transition to: intact when: burn_duration < 0;	// il n'incendie pas				
			if ( burn_duration > ( tree_propagating_time ) ) {  // les voisin est feu apres le temps = 1/5 tree_burning_time
				if ( flip(proba_propagation) ){
					let voisin <- (self neighbours_at (range)) as list where ( each.state = 'intact');
					ask voisin {								
						set state <- 'burning';								
					}					
				}
			}
		}
		state destroyed {
			if ( size > 4 ) { // r�duit les arbres qui plus grand 4m
				set over_4m <- over_4m -1;
			}						
			set size <- 3.0;
			set color <- rgb("black");
		}			
		// Pour affiche		
		aspect basic{
			draw shape: circle size: size color: color;		
		}
		aspect image{ 
			draw image: image_tree_path size: size;
		}			
	}
// objet pompier	
	species name: pompier skills:[moving]{
		rgb color <- rgb("red");
		float size <- size_of_pompier;
		point location <- [rnd (width_and_height_of_environment - 2*size_of_pompier) +size_of_pompier, rnd (width_and_height_of_environment -2*size_of_pompier) +size_of_pompier] as point;
		float range <- range_of_pompier; 
		float speed <- speed_of_pompier;		
		geometry shape <- circle(size_of_pompier) simplification (0.1);				
		
		reflex patrolling {			
			do wander speed:speed_of_pompier amplitude: 180;												
			// obtenir une list d'arbres qui dans la range de pompier et qui est 'burning'
			let voisin_tree <- ( (self neighbours_at (range)) of_species tree ) as list where ( each.state = 'burning');			
			ask voisin_tree {												
				set burn_duration <- burn_duration - capacity_of_pompier; // Le pompier �teinds l'incendie
			}
 		}		
		aspect image {
			draw image: image_pompier_path size: size;
		}
		aspect default { 			
			draw shape: circle color: color size: size ;			
		}				
	}
}

experiment tp1 type: gui {	
// category environment	
	parameter 'Width and height of environment' var: width_and_height_of_environment category: 'environment';
//	Category arbres
	parameter 'Number of arbres' var: number_of_tree category: 'arbres';
	parameter 'Size of arbres' var: size_of_tree category: 'arbres';
	parameter 'Burning range' var: burning_range category: 'arbres';
	parameter 'Tree burning time' var: tree_burning_time category: 'arbres';
	parameter 'Propagation de feu' var: tree_propagation_probability category: 'arbres'; 
	
//	Category pompier
	parameter 'Number of pompier' var: number_of_pompier category: 'pompier';
	parameter 'Size of pompier' var: size_of_pompier category: 'pompier'; 	
	parameter 'Range of pompier' var: range_of_pompier category: 'pompier';
	parameter 'Capacity of pompier' var: capacity_of_pompier category: 'pompier';
		
	output{
		inspect name: 'Species' type: species;
		inspect name: 'Agent' type: agent;
		display Forest refresh_every: 1  {
			image name:'background' file:'../images/soil.jpg';
			species tree aspect: basic;
			species pompier aspect: image;			
		}
		display tree_distribution refresh_every: 1 {
			chart td_diagram type:pie{
				data under_4m value:length (list (tree) where (each.size < 4) );
				data over_7m value:length (list (tree) where (each.size > 7) );
				data bettwen_4m_7m value:length (list (tree) where (each.size >= 4 and each.size <= 7) );								 				
			}
		}						
		monitor over_4m value: over_4m refresh_every: 1 ;	
	}
}

